package com.yb.bussiness.goods;

import java.util.List;

import com.yb.base.dao.BaseDao;
import com.yb.base.dao.IBaseDao;
import com.yb.domain.goods.TgCategory;

public class CategoryServiceImpl implements CategoryService {
	private IBaseDao<TgCategory, Long> dao;

	public CategoryServiceImpl() {
		dao = new BaseDao<TgCategory, Long>(TgCategory.class);
	}

	public String saveCategory(TgCategory category) {
		dao.saveOrUpdate(category);
		return null;
	}

	public List<TgCategory> findAll(int page, int limit) {
		if (page == -1) {
			String sql = "select * from t_g_category where level = 0 order by goodsOrder ";
			return dao.findList(sql);
		} else {
			String sql = "select * from t_g_category where level = 0 order by goodsOrder limit ?,?";
			return dao
					.findList(sql, new Object[] { (page - 1) * limit, limit });
		}
	}

	public void deleteById(Long id) {
		dao.deleteById(id);
	}

	public TgCategory findById(Long id) {
		return dao.read(id);
	}

	public List<TgCategory> findByPid(Long id, int page, int limit) {
		if (page != -1) {
			String sql = "select * from t_g_category where pid = ? order by goodsOrder desc,id desc limit ?,?";
			return dao.findList(sql, new Object[] { id, (page - 1) * limit,
					limit });
		} else {
			String sql = "select * from t_g_category where pid = ? order by goodsOrder desc,id desc";
			return dao.findList(sql, new Object[] { id });
		}
	}

	public void modify(TgCategory category) {
		dao.update(category);
	}

	public String getPidById(Long categoryId) {
		String sql = "select id from t_g_category where id=(select pid from t_g_category where id = "
				+ categoryId + ")";
		return (dao.findObjectArryList(sql).get(0)[0]).toString();
	}

	public int getCount() {
		try {
			String sql = "select * from t_g_category where level = 0";
			List<TgCategory> list = dao.findList(sql);
			if (list != null && list.size() > 0)
				return list.size();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	public int getPingouCountByCategoryId(Long categoryId) {
		String sql = "SELECT COUNT(*) FROM t_p_buy_record tbr WHERE EXISTS (SELECT 1 FROM t_pinggou tgb WHERE tgb.brandId = tbr.brandId AND EXISTS "
				+ "(SELECT 1 FROM t_g_category tgc WHERE tgc.id = tgb.categoryId AND tgc.pID = ?))";
		List<Object[]> countList = dao.findObjectArryList(sql,
				new Object[] { categoryId });
		int count = 0;
		if (null != countList && countList.size() > 0) {
			Object[] countAry = countList.get(0);
			count = new Integer(countAry[0].toString());
		}
		String goodSql = "select count(*) from t_g_brand_member tgbm"
				+ " where EXISTS (select 1 from t_g_goods tgg where tgg.id = tgbm.brandId and exists (SELECT 1 FROM t_g_category tgc WHERE tgc.id = tgg.categoryid AND tgc.pID = ?))";
		List<Object[]> countList1 = dao.findObjectArryList(goodSql,
				new Object[] { categoryId });
		int count1 = 0;
		if (null != countList1 && countList1.size() > 0) {
			Object[] countAry = countList1.get(0);
			count1 = new Integer(countAry[0].toString());
		}
		return count + count1;
	}

	public int getTotalInfoByCategoryId(Long categoryId) {
		String sql = "select count(*) from t_g_information where pcategoryId = ? and imgFlag = '2' and hot = '2'";
		List<Object[]> countList = dao.findObjectArryList(sql,
				new Object[] { categoryId });
		int count = 0;
		if (null != countList && countList.size() > 0) {
			Object[] countAry = countList.get(0);
			count = new Integer(countAry[0].toString());
		}
		return count;
	}
	
	public int getTotalInfoByCategoryIdTask(Long categoryId) {
        String sql = "select count(*) from t_g_information where PcategoryId = ? and categoryFlag = 1 and hotActivities = '2' and imgFlag = '2' and hot = '2'";
        List<Object[]> countList = dao.findObjectArryList(sql,
                new Object[] { categoryId });
        int count = 0;
        if (null != countList && countList.size() > 0) {
            Object[] countAry = countList.get(0);
            count = new Integer(countAry[0].toString());
        }
        return count;
    }

	public boolean checkDel(Long id, int level) {
		boolean flag = true;
		if (level == 1) {
			String sql = "select count(*) from t_g_category where pID = ?";
			List<Object[]> list = dao.findObjectArryList(sql,
					new Object[] { id });
			Object[] objAry = list.get(0);
			if (new Integer(objAry[0].toString()) > 0)
				flag = false;
		} else {
			String sql = "select count(*) from t_g_type where categoryId = ?";
			if (bulidResult(sql,new Object[]{id}) > 0)
				flag = false;
			if (!flag)
				return flag;
			String sql1 = "select count(*) from t_brand_type where categoryId = ?";
			if (bulidResult(sql1,new Object[]{id}) > 0)
				flag = false;
		}
		return flag;
	}

	private int bulidResult(String sql, Object[] conditionAry) {
		int count = 0;
		List<Object[]> list = dao.findObjectArryList(sql,conditionAry);
		Object[] objAry = list.get(0);
		count = new Integer(objAry[0].toString());
		return count;
	}

}
